﻿using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using static System.Formats.Asn1.AsnWriter;

namespace _065.依赖注入
{
	internal class Program
	{
		static void Main(string[] args)
		{
			ServiceCollection services = new ServiceCollection();
			services.AddScoped<ITestService, TestServiceImpl1>();
			services.AddScoped<ITestService, TestServiceImpl2>();
			using (ServiceProvider serviceProvider = services.BuildServiceProvider())
			{
				//ITestService ts1 = serviceProvider.GetService<ITestService>();
				//ts1.Name = "Test";
				//ts1.SayHi();

				// 得到多个服务
				IEnumerable<ITestService>  tests = serviceProvider.GetServices<ITestService>();
				foreach (ITestService test in tests)
				{
					Console.WriteLine(test.GetType());
				}
			}
		}

		static void Main1(string[] args)
		{
			// 创建对象
			ServiceCollection services = new ServiceCollection();
			// 添加生命周期
			//services.AddTransient<TestServiceImpl1>();
			//services.AddSingleton<TestServiceImpl1>();
			services.AddScoped<TestServiceImpl1>();
			// 构建服务提供者
			using (ServiceProvider serviceProvider = services.BuildServiceProvider())
			{
				// 得到服务
				//TestServiceImpl1 testService = serviceProvider.GetService<TestServiceImpl1>();
				//testService.Name = "tom";
				//testService.SayHi();
				//TestServiceImpl1 testService1 = serviceProvider.GetService<TestServiceImpl1>();

				//// 判断是否为同一对象
				//Console.WriteLine(object.ReferenceEquals(testService, testService1));

				TestServiceImpl1 testService;
				using (IServiceScope scope1 = serviceProvider.CreateScope())
				{
					// 在scope中获取Scope相关对象  scope.ServiceProvider 而不是ServiceProvider
					testService = scope1.ServiceProvider.GetService<TestServiceImpl1>();
					testService.Name = "tom";
					testService.SayHi();
					TestServiceImpl1 testService1 = scope1.ServiceProvider.GetService<TestServiceImpl1>();

					// 判断是否为同一对象
					Console.WriteLine(object.ReferenceEquals(testService, testService1));
				}
				TestServiceImpl1 testService22;
				using (IServiceScope scope2 = serviceProvider.CreateScope())
				{
					// 在scope中获取Scope相关对象  scope.ServiceProvider 而不是ServiceProvider
					testService22 = scope2.ServiceProvider.GetService<TestServiceImpl1>();
					testService22.Name = "tom";
					testService22.SayHi();
					TestServiceImpl1 testService1 = scope2.ServiceProvider.GetService<TestServiceImpl1>();

					// 判断是否为同一对象
					Console.WriteLine(object.ReferenceEquals(testService22, testService1));
				}

				// 判断是否为同一对象 不同的范围不是同一个对象
				Console.WriteLine(object.ReferenceEquals(testService, testService22));
			}
		}
	}


	public interface ITestService
	{
		public string Name { get; set; }
		public void SayHi();
	}

	public class TestServiceImpl1 : ITestService, IDisposable
	{
		public string Name { get; set; }

		public void Dispose()
		{
			Console.WriteLine("Dispose....");
		}

		public void SayHi()
		{
			Console.WriteLine($"Hi,I'm {Name}");
		}
	}
	public class TestServiceImpl2 : ITestService
	{
		public string Name { get; set; }

		public void SayHi()
		{
			Console.WriteLine($"你好 我是 {Name}");
		}
	}
}
